part of '../repository.dart';

const _kCollectionMetaSortingKey = 'sorting';
const _kCollectionMetaSelectionKey = 'selected';
const _kCollectionMetaHighPriorityKey = 'high_priority';

class _CollectionMetaDto extends Equatable {
  const _CollectionMetaDto._internal({
    required this.sortingDto,
    required this.selected,
    required this.highPriority,
  });

  final _CollectionSortingDto sortingDto;
  final bool selected;
  final bool highPriority;

  @override
  List<Object?> get props {
    return [
      sortingDto,
      selected,
      highPriority,
    ];
  }

  factory _CollectionMetaDto.fromJson(Map<String, dynamic> json) {
    final sortingDto = _CollectionSortingDto.fromString(json[_kCollectionMetaSortingKey]);

    return _CollectionMetaDto._internal(
      sortingDto: sortingDto,
      selected: json[_kCollectionMetaSelectionKey],
      highPriority: json[_kCollectionMetaHighPriorityKey],
    );
  }

  factory _CollectionMetaDto.fromEntity(BackupCollectionMeta collectionMeta) {
    final sortingDto = _CollectionSortingDto.fromEntity(collectionMeta.sorting);

    return _CollectionMetaDto._internal(
      sortingDto: sortingDto,
      selected: collectionMeta.selected,
      highPriority: collectionMeta.highPriority,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      _kCollectionMetaSortingKey: sortingDto.toString(),
      _kCollectionMetaSelectionKey: selected,
      _kCollectionMetaHighPriorityKey: highPriority,
    };
  }

  BackupCollectionMeta toEntity() {
    final sorting = sortingDto.toEntity();

    return BackupCollectionMeta(
      sorting: sorting,
      selected: selected,
      highPriority: highPriority,
    );
  }
}
